from flask import Flask,request
from app.Vendor.Code import Code
from app.Vendor.GetConfig import config
from app.DB.RedisHelper import RedisHelper

app = Flask(__name__)

#redis缓存
redisHelper = RedisHelper()


from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# 配置 sqlalchemy  数据库驱动://数据库用户名:密码@主机地址:端口/数据库?编码
app.config['SQLALCHEMY_DATABASE_URI'] = config.sqlalchemy_database_uri
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = config.sqlalchemy_track_modifications
#创建数据库及连接
engine = create_engine(config.sqlalchemy_database_uri)
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)
dBSession = DBSession()

@app.teardown_appcontext
def shutdown_session(exception=None):
    dBSession.close()


from app.Vendor.ExceptionApi import ExceptionApi
#挂载500异常处理,并记录日志
@app.errorhandler(Exception)
def error_handler(e):
    return ExceptionApi(Code.ERROR, e)


#引入使用的控制器
from app.Controllers.UsersController import user
app.register_blueprint(user, url_prefix='/user')


from flask_docs import ApiDoc
# 需要显示文档的 Api
app.config['API_DOC_MEMBER'] = ['user']
# 需要排除的 RESTful Api 文档
app.config['RESTFUL_API_DOC_EXCLUDE'] = []
ApiDoc(app)


#跨域
from flask_cors import CORS
CORS(app, supports_credentials=True)